package com.inmaytide.website.utils.configurations;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 容器初始化时加载properties至内存中
 * Created on 2016-9-15
 * <p>Title:       增量部署工具</p>
 * <p>Description: [容器初始化时加载properties至内存中]</p>
 * <p>Copyright:   Copyright (c) 2011</p>
 * <p>Company:     </p>
 * <p>Department:  网站运维部</p>
 *
 * @author jun
 */
public class PropertyConfigurer extends PropertyPlaceholderConfigurer {

    /**
     * 缓存
     */
    private static Map<String, String> ctxPropertiesMap;

    public static String getContextProperty(String name) {
        return ctxPropertiesMap.get(name);
    }

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);

        ctxPropertiesMap = new HashMap<String, String>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            ctxPropertiesMap.put(keyStr, value);
        }
    }

}

